package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import qlsl.androiddesign.activity.commonactivity.EditActivity;
import qlsl.androiddesign.activity.commonactivity.SelectCityActivity;
import qlsl.androiddesign.activity.subactivity.PostJobActivity;
import qlsl.androiddesign.adapter.subadapter.PostJobAdapter;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.entity.commonentity.City;
import qlsl.androiddesign.entity.otherentity.RecruitList;
import qlsl.androiddesign.http.service.subservice.PostJobService;
import qlsl.androiddesign.view.subview.activityview.peoplecenterbase.ListWapBaseView;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 发布求职页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ： FunctionView<PostJobActivity>
 */
public class PostJobView extends
		ListWapBaseView<HashMap<String, Object>, PostJobActivity> {

	public PostJobView(PostJobActivity activity) {
		super(activity);
	}

	protected void initView(View view) {
		super.initView(view);
		setTitle("发布求职");
	}

	protected void initData() {
		setListViewData();
		super.initData();

	}

	protected void setListViewData() {
		String[] names = new String[] { "标题", "招聘地区", "驾驶时长", "薪资标准", "工作经验",
				"掌握技能", "操作方向", "福利要求", "手机号" };

		Integer[] icons = new Integer[] { 1, 1, 1, 1, 1, null, null, null, 1 };

		for (int index = 0; index < names.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("icon", icons[index]);
			map.put("text", names[index]);
			map.put("value", "");
			list.add(map);
		}
		/*
		 * PostJobAdapter adapter = ; listView.setAdapter(adapter);
		 */
	}

	protected void initListener() {
	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list_item:
			doClickListItem(view);

			break;
		case R.id.send:
			doClickSendPost(view);
			break;
		}
	}

	public void doClickSendPost(View view) {
		for (HashMap<String, Object> map : list) {
			Object value = map.get("value");
			if (value == null || TextUtils.isEmpty(value.toString())) {
				showToast("请输入");
				return;
			}
		}
		PostJobService.insertAdvertises("2", list, this, activity);

	}

	@SuppressWarnings("unchecked")
	public void doClickListItem(View view) {
		int position = listView.getPositionForView(view);
		listView.setTag(position);
		if (position == 1) {
			Intent intent0 = new Intent(activity, SelectCityActivity.class);
			startActivityForResult(intent0, Constant.SELECT_CITY_RESULT_CODE);
		} else {
			HashMap<String, Object> map = (HashMap<String, Object>) listView
					.getAdapter().getItem(position);
			String text = (String) map.get("text");
			String value = (String) map.get("value");

			Intent intent = new Intent(activity, EditActivity.class);
			intent.putExtra("title", "编辑" + text);
			intent.putExtra("maxLength", 11);
			intent.putExtra("content", value);

			startActivityForResult(intent, 0);
		}
	}

	public BaseAdapter getAdapter() {
		return new PostJobAdapter(activity, list);
	}

	public void onActivityResult(int requestCode, int arg1, Intent data) {
		HashMap<String, Object> map = (HashMap<String, Object>) listView
				.getAdapter().getItem((Integer) listView.getTag());
		if (requestCode == 0) {
			if (data != null) {
				map.put("value", data.getStringExtra("content"));
			}
		} else if (requestCode == Constant.SELECT_CITY_RESULT_CODE) {
			if (data != null) {
				City city = (City) data.getExtras().get("city");
				City city2 = (City) data.getExtras().get("city2");
				// StringBuffer append = new StringBuffer();
				String citys = "";
				if (city2 != null) {
					citys = city2.getArea_name()
							+ activity.getString(R.string.single);
					// append.append(city2.getArea_name());
				}
				if (city != null) {
					citys += city.getArea_name();
					// append.append(city.getArea_name());
					map.put("value", citys);
				}
			}
		}
		notifyDataSetChanged();

	}
}
